package com.topone.controller;

import com.topone.Enum.CouponEnum;
import com.topone.Enum.TcUserCouponEnum;
import com.topone.Exception.FanZoneException;
import com.topone.dataObject.TcUser;
import com.topone.dataObject.TcUserCoupon;
import com.topone.dto.BussinessDTO;
import com.topone.dto.TcUserDTO;
import com.topone.service.TcUserCouponService;
import com.topone.service.TcUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/1/18,0018.
 */
@CrossOrigin
@Controller
@RequestMapping("/")
@Slf4j
public class
RequestController {
    @Autowired
    private TcUserService TcUserService;

    @Autowired
    private TcUserCouponService tcUserCouponService;

    //登陆首页(带参数)
    @GetMapping("buyer/wxIndex")
    public ModelAndView wxIndex(
                                Map<String,Object> map){

         map.put("openid","oeeRzxLaCin4AcZ5_K7ie7S5KPng");
         log.info("openid={}",map);
        return new ModelAndView("buyer/wx_index",map);
    }

    //登陆首页
    @GetMapping("buyer/wx_index")
    public ModelAndView wx_index(){

        return new ModelAndView("buyer/wx_index");
    }


    //商家名搜索
    @GetMapping("query")
    public ModelAndView query(){

        return new ModelAndView("buyer/query");
    }

    //福利专区

    @GetMapping("welfareZone")
    public  ModelAndView welfareZone(){

        return new ModelAndView("buyer/WelfareZone");
    }

    //个人中心
    @GetMapping("personalInfo")
    public ModelAndView personalInfo(@RequestParam("openid") String openid,
                                     Map<String ,Object> map){
        try {
            TcUserDTO tcUser= TcUserService.findUserInfoByopenid(openid);
            map.put("tcUser",tcUser);
        }catch(FanZoneException e){
            log.error("【根据openid查询用户信息】失败,e={}",e.getMessage());
        }
        log.info("【根据openid查询用户信息】成功,{}",map);
        return new ModelAndView("/buyer/personal_info",map);
    }

    //优惠券
    @GetMapping("couponCenter")
    public  ModelAndView  couponCenter(@RequestParam("openid") String openid,
                                       Map<String,Object> map){
        if(StringUtils.isEmpty(openid)){
            log.error("【查询优惠券】openid为空");
            map.put("error","【查询优惠券】openid为空");
            return new ModelAndView("/buyer/personal_info",map);
        }
        try {
            TcUserCoupon tcUserCoupon=new TcUserCoupon();
            tcUserCoupon.setBuyerOpenid(openid);
            tcUserCoupon.setUseStatus(TcUserCouponEnum.UNUSE.getCode());
            List<BussinessDTO> userCouponList= tcUserCouponService.findCouponByopenid(tcUserCoupon);
            map.put("userCouponList",userCouponList);
        }catch (FanZoneException e){
            log.error("【查询用户优惠券】失败，e={}",e);
            map.put("error",e.getMessage());
            return new ModelAndView("/buyer/personal_info",map);
        }

        map.put("flag","#inactive_target");
        map.put("success","查询成功");
        log.info("【查询用户优惠券】成功 {}",map);
        return new ModelAndView("/buyer/coupan_center",map);
    }

    //添加便民信息

    @GetMapping("addConveintInfo")
    public ModelAndView addConvenientInfo() {

        return new ModelAndView("/buyer/addService");
    }

    //我的关注
    @GetMapping("myAttention")
    public ModelAndView myAttention(){

        return new ModelAndView("/buyer/myAttention");
    }


    //我的收藏
    @GetMapping("myFavorate")
    public ModelAndView myFavorate(){

        return new ModelAndView("/buyer/myFavorate");
    }

     //玩转积分
    @GetMapping("myIntegral")
    public ModelAndView myIntegral(){

        return new ModelAndView("/buyer/myIntegral");
    }

    //浏览记录
    @GetMapping("browsingHistory")
    public ModelAndView browsingHistory(){

        return new ModelAndView("/buyer/browsingHistory");
    }
}
